﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
using System.Windows.Controls;
namespace WPFExcelReport
{
    public partial class TSLS02
    {

        public TSLS02()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VLIEFDAT();
            this.DataContext = MV;
            InitializeComponent();
        }


        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                QTYP = "0";
            }
            else
            {
                QTYP = dwMaster["LSFRAGRE"].ToString();
            }
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            LOADALL();
            base.Window_Loaded(sender, e);

        }
        public override void LOADALL()
        {
            if (!DBSDDAT.ContainsKey("LS02"))
            {
                SResult = TMV.QueryLIEFDAT(dwData, -1);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    row["KEY"] = Helpers.Helper.GetGB2312Value(row["LSLIEFNA"].ToString());
                }
                MergeData(SResult, false);
                Locate("LSLIEFNR", KEY);
                DataTable tb = TBMain.Copy();
                foreach (DataRow row in tb.Rows)
                {
                    row["DES"] = row["LSLIEFNA"];
                }
                DBSDDAT["LS02"] = tb;
            }
            else
            {
                TBMain.Clear();
                TBMain.Merge(DBSDDAT["LS02"].Copy());
            }
        }
        public override void OnReSetChg()
        {
            if (!IsReset)
                return;
            if (DBSDDAT.ContainsKey("LS02"))
            {
                DBSDDAT.Remove("LS02");
            }
            base.OnReSetChg();
        }
        public void TextBlock_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            /*
            if (e.Key != System.Windows.Input.Key.Enter)
                return;
            if (e.Key != System.Windows.Input.Key.Return)
                return;*/
            this.Focus();
            TextBox tb = sender as TextBox;

            filter = "LSLIEFNA LIKE '%" + ART.ToUpper().Trim() + "%' OR LSNAME1 LIKE '%"
                + Helpers.Helper.ConvertGB2312To8859P1(ART).Trim() + "%'";
            cvMaster.CustomFilter = filter;
            vwMaster.View.MoveCurrentTo(-1);
            vwMaster.View.MoveCurrentToFirst();
            cSKD.IsDropDownOpen = true;
            /*rows = TBMain.Select(filter);
            if (rows.Length > 0)
            {
                //Locate("KDKUNDNR", rows[0]["KDKUNDNR"].ToString());
            }*/

            tb.Focus();
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            PM["LSLIEFNA"] = Helpers.Helper.Left(ART.Trim().PadRight(8), 8);
            TMV.NEWLS(PM);
            IsReset = false;
            IsReset = true;
            LOADALL();
            base.NewCommand(sender, e);
        }
        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            cSKD.IsDropDownOpen = false;
        }
        public override void LOADBYAUF()
        {
            Locate("LSLIEFNR", ORDERNUM);
        }
        public override void ManageCommand(object sender, RoutedEventArgs e)
        {
            if (IsManage)
            {
                LOADALL();
            }
            else
            {
                VICC_WFEVENTS.SVWF.LS02(this.RUNID);
            }
            base.ManageCommand(sender, e);
        }
    }
}
